<template>
  <div class="risk app-container">
    <div class="mb-20">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table v-loading="loading" :data="list" style="width: 100%;">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="分销级别" align="center" prop="level" >
        <template slot-scope="scope">
          <span>{{ scope.row.level }} 级</span>
        </template>
      </el-table-column>
      <el-table-column label="分销百分比" align="center" prop="proportion">
        <template slot-scope="scope">
          <span>{{ scope.row.proportion }} %</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
      <!-- 修改分销 -->
      <el-dialog :title="title" :visible.sync="open" width="590px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="分销百分比" prop="proportion">
              <el-input type="number" v-model="form.proportion" :min="0" placeholder="请输入红包数量" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getDistribution , putDistribution} from "@/api/system/distribution";

export default {
  name: "distribution",
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: true,
      // 表格数据
      list: [],
      title: '',
      open: false,
      // 查询参数
      queryParams: {
      },
       // 表单校验
       rules: {
        proportion: [
          { required: true, message: "请输入分销百分比", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getDistribution(this.queryParams).then(response => {
        this.list = response.data;
        this.loading = false;
      });
    },

      // 表单重置
    reset() {
      this.form = {
        id: undefined,
        proportion: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleUpdate(row) {
      this.reset();
      this.form = {
        id: row.id,
        proportion: row.proportion,
      };
      this.open= true;
      this.title = '修改分销'
    },
      /** 提交按钮 */
      submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          putDistribution(this.form).then(response => {
              this.$modal.msgSuccess("修改分销成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
  }
};
</script>
